//
//  AppAlertViewController.swift
//  app
//
//  Created by g1 on 2023/9/20.
//

import UIKit

class AppAlertController: UIView {
    
    private let rootView = UIApplication.shared.keyWindow!
    private var backgroundView: UIView!
    
    private var leftHolder: (()->Void)?
    private var rightHolder: (()->Void)?
    
    private let backViewWith: CGFloat = { screenFixationWidth - 60 * 2 }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        configUI()
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        DispatchQueue.main.async {
            self.backView.layerCornerRadius(12)
        }
    }
    
    convenience init(title: String, message: String, leftTitle: String = "取消".localized, rightTitle: String = "确认".localized, leftBtnTitleColor: UIColor = .app_promptTextColor, rightBtnTitleColor: UIColor = .app_black, leftHolder: (()->Void)? = nil, rightHolder: (()->Void)? = nil) {
        self.init(frame: .zero)

        titleLabel.text = title
        contentLabel.text = message
        
        leftBtn.setTitle(leftTitle)
        rightBtn.setTitle(rightTitle)
        
        leftBtn.setTitleColor(leftBtnTitleColor)
        rightBtn.setTitleColor(rightBtnTitleColor)
        
        self.leftHolder = leftHolder
        self.rightHolder = rightHolder
        
    }
    
    // MARK: - UIButtonAction
    @objc func leftBtnAction() {
        
        if let holder = leftHolder {
            holder()
        }
        
        hide()
    }
    @objc func rightBtnAction() {
        
        if let holder = rightHolder {
            holder()
        }
        
        hide()
    }
    
    // MARK: - configUI
    func configUI() {
        
        addSubview(backView)
        backView.addSubview(titleLabel)
        backView.addSubview(contentLabel)
        backView.addSubview(leftBtn)
        backView.addSubview(rightBtn)

        backView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(screenFixationWidth-84)
        }
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(30)
            make.left.equalTo(38)
            make.right.equalTo(-38)
            make.height.equalTo(22)
        }
        contentLabel.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(24)
            make.left.right.equalTo(titleLabel)
        }
        leftBtn.snp.makeConstraints { make in
            make.top.equalTo(contentLabel.snp.bottom).offset(36)
            make.left.equalToSuperview()
            make.width.equalTo(contentLabel).offset(-6)
            make.height.equalTo(CGFloat.app_btnHeight)
            make.bottom.equalTo(-32)
        }
        rightBtn.snp.makeConstraints { make in
            make.right.equalToSuperview()
            make.width.centerY.height.equalTo(leftBtn)
        }
        
        leftBtn.addTarget(self, action: #selector(leftBtnAction))
        rightBtn.addTarget(self, action: #selector(rightBtnAction))
    }
    private lazy var backView: UIView = {
        let backView = UIView()
        backView.backgroundColor = .white
        return backView
    }()
    private lazy var titleLabel: UILabel = {
        let titleLabel = UILabel()
        titleLabel.font = kSetPingFangBlod(16)
        titleLabel.textAlignment = .center
        titleLabel.textColor = .app_black
        return titleLabel
    }()
    private lazy var contentLabel: UILabel = {
        let contentLabel = UILabel()
        contentLabel.textAlignment = .center
        contentLabel.textColor = .app_black
        contentLabel.font = kSetPingFangRegular(16)
        contentLabel.numberOfLines = 0
        return contentLabel
    }()
    lazy var leftBtn: CustomClickableView = {
        let leftBtn = CustomClickableView()
        leftBtn.setFont(kSetPingFangRegular(14))
        leftBtn.setTitleColor(.app_black)
        leftBtn.layer.cornerRadius = CGFloat.app_btnHeight/2
        leftBtn.layer.borderWidth = 1
        leftBtn.layer.borderColor = UIColor(hex: 0xEEEEEE).cgColor
        leftBtn.layer.masksToBounds = true
        return leftBtn
    }()
    private lazy var rightBtn: GradientButton = {
        let rightBtn = GradientButton()
        rightBtn.setFont(kSetPingFangRegular(14))
        rightBtn.gradientColors = UIColor.app_themeGradientColors
        rightBtn.layer.cornerRadius = CGFloat.app_btnHeight/2
        rightBtn.layer.masksToBounds = true
        return rightBtn
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension AppAlertController {
    
    func show() {
        // 防止多个 AlertView 重复显示
        if rootView.subviews.filter({ $0.isKind(of: AppAlertController.self) }).count > 0 { return }
                
        backgroundView = UIView()
        backgroundView.backgroundColor = UIColor.black.withAlphaComponent(0)
        rootView.addSubview(backgroundView)
        
        backgroundView.snp.makeConstraints { (make) in
            make.edges.equalTo(rootView)
        }
        
        self.alpha = 0
        rootView.addSubview(self)
        
        snp.makeConstraints { (make) in
            make.width.equalTo(screenWidth-30)
            make.height.equalTo(350)
            make.center.equalToSuperview()
        }
        
        rootView.setNeedsLayout()
        rootView.layoutIfNeeded()
                
        UIView.animate(withDuration: 0.35, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 5, options: UIView.AnimationOptions(rawValue: 0), animations: {
            self.alpha = 1
            self.backgroundView.backgroundColor = UIColor.black.withAlphaComponent(0.6)
        }, completion: nil)
    }
    
    @objc private func hideAction() {
        hide()
    }
    
    private func hide(completion: (() -> Void)? = nil) {
        UIView.animate(withDuration: 0.35, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 5, options: UIView.AnimationOptions(rawValue: 0), animations: {
            self.alpha = 0
            self.backgroundView.backgroundColor = UIColor.black.withAlphaComponent(0)
        }) { (finished) in
            guard finished else { return }
            self.removeFromSuperview()
            self.backgroundView.removeFromSuperview()
        }
    }
        
}

